<?php
/**
 * Gerador de Models
 *
** @author Níssius Ribas 
 */
class gerador extends NT_Default_Controller {

    private $seo;
    
    public function __construct() {
        parent::__construct();
        
        $this->load->library("mobile_detect"); // apagar se o site não tem mobile
    }
    
    public function index($lang = null) {
        $bd = $this->db->database;
        
        $sql = "SHOW 
                    TABLES
                FROM
                    $bd
                WHERE 
                    Tables_in_$bd NOT LIKE '%global%' 
                    and  Tables_in_$bd NOT LIKE '%manager%'
                    and Tables_in_$bd NOT LIKE '%nt_gd%'
                    and Tables_in_$bd != 'nt_site_twitter'";
        
        echo $sql;
        $tabelas = $this->db->query($sql)->result_array();
        foreach($tabelas as $tabela){
            $sql = "SELECT 
                            COLUMN_NAME,
                            IF(IS_NULLABLE = 'NO','required','') as obrigatorio,
                            IFNULL(CHARACTER_MAXIMUM_LENGTH,'') as tamanho_maximo,
                            COLUMN_COMMENT as label
                    FROM 
                            information_schema.columns 
                    WHERE 
                            table_name = '".$tabela['Tables_in_'.$bd]."'
                            AND COLUMN_NAME <> 'ID'
                            AND TABLE_SCHEMA = '$bd'";

            $campos = $this->db->query($sql)->result_array();
            $campos_unidos = '';
            foreach($campos as $campo){
                $regra='';
                if($campo['obrigatorio'] != ''){
                    if($campo['tamanho_maximo'] != ''){
                        $regra = $campo['obrigatorio'].'|max_length['.$campo['tamanho_maximo'].']';
                    }else{
                        $regra = $campo['obrigatorio'];
                    }
                }else{
                     if($campo['tamanho_maximo'] != ''){
                          $regra = 'max_length['.$campo['tamanho_maximo'].']';
                     }
                }
                $campos_unidos .=  "array('field' => '$campo[COLUMN_NAME]', 'label' => '$campo[label]', 'rules' =>  '$regra'),\n\t\t\t";
            }
            $campos_unidos = substr($campos_unidos,0,-1);
            $data_gerado = date(DATE_RFC822);
            $conteudo = <<<EOD
<?php
/**
 * Gerador Automático
 * @author Níssius Ribas <nissius@noiatec.com.br>
 * Gerado em: $data_gerado
 */
class {$tabela['Tables_in_'.$bd]} extends NT_Model {
        private \$validation = array(
            $campos_unidos
        );
    /**
    * Devolve o array com as validações para esta model
    * 
    * @return array com as regras de validação desta model
    */
    public function getRules(){
       return \$this->validation;
    }
}
    
EOD;
            //unlink('application/models/'.$tabela['Tables_in_'.$bd].'.php');
            $file = @fopen('application/models/'.$tabela['Tables_in_'.$bd].'.php', "x+");
            @fwrite($file, stripslashes($conteudo));
            @fclose($file);
        }
    }

}
